package com.ysh.poi;

import com.ysh.domain.store.Question;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/*
    报表制作
        模板表头
        模板标题
        模板数据
 */
public class PoiTest2 {
    /*
        报表骨架，表头、标题、数据区
     */
    @Test
    public void testProjectPoi() throws IOException {
        // 1.获取到对应的Excel文件，工作簿文件
        Workbook wb = new XSSFWorkbook();
        // 2.创建工作区
        Sheet s = wb.createSheet("题目数据文件表");

        // 设置通用配置
        // 索引为4字段的的单元格宽度
        // s.setColumnWidth(4,120);
        // 定义属性
        CellStyle cs_field = wb.createCellStyle();
        // 水平居中
        cs_field.setAlignment(HorizontalAlignment.CENTER);
        // 黑体
        cs_field.setBorderTop(BorderStyle.THIN);
        // 黑体向下对齐
        cs_field.setBorderBottom(BorderStyle.THIN);
        cs_field.setBorderLeft(BorderStyle.THIN);
        cs_field.setBorderRight(BorderStyle.THIN);
        /*
             制作标题
         */
        // CellRangeAddress四个参数：开始行，结束行，开始列，结束列
        s.addMergedRegion(new CellRangeAddress(1,1,1,12));
        /*
            标题第2行第2列创建并写入数据
         */
        // 创建第1行,索引为1，即第2行位置
        Row row_1 = s.createRow(1);
        // 创建列
        Cell cell_1_1 = row_1.createCell(1);
        cell_1_1.setCellValue("在线试题导出信息");
        // 创建一个样式
        CellStyle cs_title = wb.createCellStyle();
        // 内容水平居中
        cs_title.setAlignment(HorizontalAlignment.CENTER);
        // 内容垂直居中
        cs_title.setVerticalAlignment(VerticalAlignment.CENTER);
        // 为cell_1_1添加上面定义的样式
        cell_1_1.setCellStyle(cs_title);

        /*
            制作表头
         */
        String[] fields = {"题目ID","所属公司ID","所属目录ID","题目简介","题干描述",
                "题干配图","题目分析","题目类型","题目难度","是否经典题","题目状态","审核状态"};
        // 创建第2行列表
        Row row_2 = s.createRow(2);
        for (int i = 0; i < fields.length; i++) {
            Cell cell_2_temp = row_2.createCell(1+i);
            cell_2_temp.setCellValue(fields[i]);
            // 设置属性
            cell_2_temp.setCellStyle(cs_field);
        }

        /*
             制作数据区
         */
        List<Question> questionList = new ArrayList<>();
        Question qq = new Question();
        qq.setId("1");
        qq.setPicture("12");
        qq.setReviewStatus("13");
        qq.setAnalysis("14");
        qq.setCatalogId("15");
        qq.setCompanyId("16");
        qq.setDifficulty("17");
        qq.setIsClassic("18");
        qq.setRemark("19");
        qq.setState("21");
        qq.setSubject("31");
        qq.setType("41");
        questionList.add(qq);
        Question qqq = new Question();
        qqq.setId("1");
        qqq.setPicture("12");
        qqq.setReviewStatus("13");
        qqq.setAnalysis("14");
        qqq.setCatalogId("15");
        qqq.setCompanyId("16");
        qqq.setDifficulty("17");
        qqq.setIsClassic("18");
        qqq.setRemark("19");
        qqq.setState("21");
        qqq.setSubject("31");
        qqq.setType("41");
        questionList.add(qqq);

        // 行索引
        int row_index = 0;
        for (Question question : questionList) {
            // 每次执行置空,列索引
            int cell_index = 0;
            // 创建行
            Row row_temp = s.createRow(3+row_index++);
            // 创建列
            Cell cell_data_1 = row_temp.createCell(1+cell_index++);
            cell_data_1.setCellValue(qq.getId());
            cell_data_1.setCellStyle(cs_field);

            Cell cell_data_2 = row_temp.createCell(1 + cell_index++);
            cell_data_2.setCellValue(qq.getCompanyId());    //++
            cell_data_2.setCellStyle(cs_field);

            Cell cell_data_3 = row_temp.createCell(1 + cell_index++);
            cell_data_3.setCellValue(qq.getCatalogId());    //++
            cell_data_3.setCellStyle(cs_field);

            Cell cell_data_4 = row_temp.createCell(1 + cell_index++);
            cell_data_4.setCellValue(qq.getRemark());    //++
            cell_data_4.setCellStyle(cs_field);

            Cell cell_data_5 = row_temp.createCell(1 + cell_index++);
            cell_data_5.setCellValue(qq.getSubject());    //++
            cell_data_5.setCellStyle(cs_field);

            Cell cell_data_6 = row_temp.createCell(1 + cell_index++);
            cell_data_6.setCellValue(qq.getPicture());    //++
            cell_data_6.setCellStyle(cs_field);

            Cell cell_data_7 = row_temp.createCell(1 + cell_index++);
            cell_data_7.setCellValue(qq.getAnalysis());    //++
            cell_data_7.setCellStyle(cs_field);

            Cell cell_data_8 = row_temp.createCell(1 + cell_index++);
            cell_data_8.setCellValue(qq.getType());    //++
            cell_data_8.setCellStyle(cs_field);

            Cell cell_data_9 = row_temp.createCell(1 + cell_index++);
            cell_data_9.setCellValue(qq.getDifficulty());    //++
            cell_data_9.setCellStyle(cs_field);

            Cell cell_data_10 = row_temp.createCell(1 + cell_index++);
            cell_data_10.setCellValue(qq.getIsClassic());    //++
            cell_data_10.setCellStyle(cs_field);

            Cell cell_data_11 = row_temp.createCell(1 + cell_index++);
            cell_data_11.setCellValue(qq.getState());    //++
            cell_data_11.setCellStyle(cs_field);

            Cell cell_data_12 = row_temp.createCell(1 + cell_index++);
            cell_data_12.setCellValue(qq.getReviewStatus());    //++
            cell_data_12.setCellStyle(cs_field);
        }


        // 创建一个文件对象，作为excel文件内容的输出文件
        File file = new File("test2.xlsx");
        // 输出时通过流的形式对外输出，包装对应的目标文件
        OutputStream os = new FileOutputStream(file);
        // 将内存中的workbook数据写入到流中
        wb.write(os);
        // 释放资源
        wb.close();
        os.close();
    }
}
